package com.thread.www.demo;

import java.util.Map;
import java.util.concurrent.LinkedTransferQueue;

/**
 * Created by wangyang on 2017/10/21.
 */
public class ThreadDumpBuilder {
    public static String build(){
        StringBuilder output = new StringBuilder(1000);
            for(Map.Entry stackTrace:Thread.getAllStackTraces().entrySet()){
                appendThreadStackTrace(output,(Thread)stackTrace.getKey(),
                (StackTraceElement[]) stackTrace.getValue());
            } 
            return output.toString();
        }
    private static void appendThreadStackTrace(StringBuilder output,Thread thread, StackTraceElement[]stack){
        //忽略当前线程的堆栈信息
        if(thread.equals(Thread.currentThread())){
            return;
        }
        output.append(thread).append("n");
        for(StackTraceElement element:stack){
            output.append("t").append(element).append("n");
        }
    }

    public static void main(String[] args) {
        LinkedTransferQueue queue = new LinkedTransferQueue();
        System.out.println(build());
    }
}
